/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadInternacao.java
 *
 * Created on 19/05/2012, 21:01:55
 */
package View;

import Dao.SistemaDAO;
import Modelo.Internacao;
import Modelo.Leito;
import Modelo.TabelaCadInternacao;
import Modelo.TabelaInternLeito;
import Util.PAPUtil;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**  
 *
 * @author Rafael
 */
public class CadInternacao extends javax.swing.JFrame {
    SistemaDAO sisDao = new SistemaDAO();
    PAPUtil papUtil = new PAPUtil();
    ArrayList<Leito> arrGlobal = new ArrayList<Leito>();
    LocAtmMedico atm = new LocAtmMedico();
    AtendimentoMedico localInternacao;
    
    /** Creates new form CadInternacao */
    public CadInternacao() {
        initComponents(); 
    }
    
 public CadInternacao(LocAtmMedico loc, AtendimentoMedico t) {
        initComponents();
        localInternacao =t;
        atm = loc;
        jTbLeitos.setModel(new TabelaInternLeito());
        arrGlobal = (ArrayList<Leito>) sisDao.listar("Modelo.Leito");
       for (Leito cad : arrGlobal) {
                TabelaInternLeito lm = (TabelaInternLeito) jTbLeitos.getModel();
                lm.addLeito(cad);
            }
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTbLeitos = new javax.swing.JTable();
        jBtConfirmarInternacao = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTaDescricaoInternacao = new javax.swing.JTextArea();
        jBtCancelarInternacao = new javax.swing.JButton();
        jLbErrMsg = new javax.swing.JLabel();
        jMbPrincipal = new javax.swing.JMenuBar();
        jMnAcoes = new javax.swing.JMenu();
        jMiCadastrar = new javax.swing.JMenuItem();
        jMiSair = new javax.swing.JMenuItem();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Leitos Disponivéis:");

        jLabel2.setText("Descrição:");

        jTbLeitos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Leitos"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane5.setViewportView(jTbLeitos);

        jBtConfirmarInternacao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/V.png"))); // NOI18N
        jBtConfirmarInternacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtConfirmarInternacaoActionPerformed(evt);
            }
        });

        jTaDescricaoInternacao.setColumns(20);
        jTaDescricaoInternacao.setRows(5);
        jScrollPane1.setViewportView(jTaDescricaoInternacao);

        jBtCancelarInternacao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/X.png"))); // NOI18N
        jBtCancelarInternacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtCancelarInternacaoActionPerformed(evt);
            }
        });

        jLbErrMsg.setText("Hostipal Reat In Peace");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jBtConfirmarInternacao, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jBtCancelarInternacao, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(37, 37, 37))
                            .addComponent(jLabel2))
                        .addGap(0, 21, Short.MAX_VALUE))
                    .addComponent(jLbErrMsg, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(7, 7, 7)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jBtCancelarInternacao, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                            .addComponent(jBtConfirmarInternacao, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 47, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(jLbErrMsg))
        );

        jMnAcoes.setText("Ações");
        jMnAcoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMnAcoesActionPerformed(evt);
            }
        });

        jMiCadastrar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMiCadastrar.setText("Cadastrar");
        jMnAcoes.add(jMiCadastrar);

        jMiSair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        jMiSair.setText("Sair");
        jMnAcoes.add(jMiSair);

        jMbPrincipal.add(jMnAcoes);

        setJMenuBar(jMbPrincipal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMnAcoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMnAcoesActionPerformed
        jLbErrMsg.setText("Especialidade cadastrada com sucesso.");
    }//GEN-LAST:event_jMnAcoesActionPerformed

    private void jBtCancelarInternacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtCancelarInternacaoActionPerformed
        localInternacao.setEnabled(true);
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jBtCancelarInternacaoActionPerformed

    private void jBtConfirmarInternacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtConfirmarInternacaoActionPerformed
        // TODO add your handling code here:
          if(jTbLeitos.getSelectedRow() < 0){
            JOptionPane.showMessageDialog(rootPane,"Selecione uma Linha");
        }else{
              localInternacao.setEnabled(true);
                TabelaInternLeito lm = (TabelaInternLeito) jTbLeitos.getModel();
                Internacao intern = new Internacao();
                Leito l = new Leito();
                l = lm.getSelectedLeito(jTbLeitos.getSelectedRow());
                l.setDisponivel(true);
                intern.setDescricao(jTaDescricaoInternacao.getText());
                intern.setLeitoIdleito(l.getIdleito());
                intern.setConsultaIdconsulta(atm.obj.getIdconsulta());
                intern.setStatus(true);
                sisDao.addUpObjeto(intern);
                sisDao.addUpObjeto(l);
                this.dispose();
                this.setVisible(false);
                
          }
    }//GEN-LAST:event_jBtConfirmarInternacaoActionPerformed

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtCancelarInternacao;
    private javax.swing.JButton jBtConfirmarInternacao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLbErrMsg;
    private javax.swing.JMenuBar jMbPrincipal;
    private javax.swing.JMenuItem jMiCadastrar;
    private javax.swing.JMenuItem jMiSair;
    private javax.swing.JMenu jMnAcoes;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTextArea jTaDescricaoInternacao;
    private javax.swing.JTable jTbLeitos;
    // End of variables declaration//GEN-END:variables
}
